const express = require("express");
const Send = require("../utils/Send");
const Utils = require("../utils");
const Code = require("../verification-code");
const Token = require("../utils/Token");

const router = express.Router();

/** 获取图形验证码 */
router.get("/code", async (req, res, next) => {
  try {
    // 从headers获取客户端唯一标识
    const { client_uuid } = Utils.getBaseParams(req);

    const { data, uuid } = await Code.getCode(client_uuid);
    res.send(Send.success({ data, uuid }));
  } catch (error) {
    next(error);
  }
});

/** 获取邮箱验证码 */
router.get("/emailCode", async (req, res, next) => {
  // 获取客户端唯一标识
  const { client_uuid } = Utils.getBaseParams(req);
  // 获取邮箱
  const { email } = req.query;
  try {
    await Code.sendEmailCode(client_uuid, email);
    res.send(Send.success());
  } catch (error) {
    next(error);
  }
});

/** 发送短信验证码 */
router.get("/smsCode", async (req, res, next) => {
  // 获取客户端IP地址
  const { client_ip } = Utils.getBaseParams(req);
  // 获取手机号码
  const { phone_number } = req.query;
  try {
    const result = await Code.sendPhoneCode(phone_number, client_ip);
    res.send(Send.success(result));
  } catch (error) {
    next(error);
  }
});



module.exports = router;
